package com.ttg.web.task;

import com.ttg.common.dto.PrimitiveBaseDTO;
import com.ttg.common.dto.TaskBaseDTO;
import com.ttg.common.exception.BaseException;
import com.ttg.common.utils.JsonUtils;
import com.ttg.common.vo.TaskResultVO;
import com.ttg.web.service.job.primitive.BankAccountPrimitiveService;
import com.ttg.web.service.job.primitive.MerchTradePrimitiveService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.context.XxlJobContext;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;

/**
 * Description： 处理收单平台-商户交易原始数据任务
 * Author: chenyou
 * Date: 2025/10/13 13:49
 * Company: ttg
 * Copyright: Copyright (c) 2025
 */
@Component
@EnableScheduling
@Slf4j
public class PrimitiveJobTask {

    @Autowired
    private BankAccountPrimitiveService bankAccountPrimitiveService;
    @Autowired
    private MerchTradePrimitiveService merchTradePrimitiveService;

    @XxlJob("bankAccountPrimitive")
    @Async
    public ReturnT<String> bankAccountPrimitive() {
        String param = XxlJobHelper.getJobParam();
        log.info("【JobTask bankAccountPrimitive 处理收单平台-每日银行账户原始数据任务】 start=>{}",param);
        try {
            PrimitiveBaseDTO reqDto = new PrimitiveBaseDTO();
            if (StringUtils.isNotBlank(param)) {
                reqDto = JsonUtils.jsonToObject(param, PrimitiveBaseDTO.class);
            }
            TaskResultVO resultVO = bankAccountPrimitiveService.saveAcctPrimitive(reqDto);
        } catch (Exception e) {
            log.error("异常调度异常：{}",e.getMessage());
            XxlJobContext.getXxlJobContext().setHandleCode(ReturnT.FAIL_CODE);
            XxlJobContext.getXxlJobContext().setHandleMsg("执行异常，请联系管理员查询相关日志" + e.getMessage());
            throw new BaseException("异常调度异常",e.getLocalizedMessage());
        }
        log.info("【JobTask bankAccountPrimitive 处理收单平台-每日银行账户原始数据任务】 end");
        return ReturnT.SUCCESS;
    }

    @XxlJob("merchTradePrimitive")
    @Async
    public ReturnT<String> merchTradePrimitive() {
        String param = XxlJobHelper.getJobParam();
        log.info("【JobTask merchTradePrimitive 处理收单平台-每日商户交易原始数据任务】 start=>{}",param);
        try {
            PrimitiveBaseDTO reqDto = new PrimitiveBaseDTO();
            if (StringUtils.isNotBlank(param)) {
                reqDto = JsonUtils.jsonToObject(param, PrimitiveBaseDTO.class);
            }
            TaskResultVO resultVO = merchTradePrimitiveService.saveMerchTradePrimitive(reqDto);
        } catch (Exception e) {
            log.error("异常调度异常：{}",e.getMessage());
            XxlJobContext.getXxlJobContext().setHandleCode(ReturnT.FAIL_CODE);
            XxlJobContext.getXxlJobContext().setHandleMsg("执行异常，请联系管理员查询相关日志" + e.getMessage());
            throw new BaseException("异常调度异常",e.getLocalizedMessage());
        }
        log.info("【JobTask merchTradePrimitive 处理收单平台-每日商户交易原始数据任务】 end");
        return ReturnT.SUCCESS;
    }
}
